package com.timeriver.machine_learning.binaryclassification

import org.apache.spark.ml.PipelineModel
import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
  * 使用训练好的逻辑回归模型进行预测
  */
object LogisticRegressionPredict {
  def main(args: Array[String]): Unit = {

    val session: SparkSession = SparkSession.builder()
      .master("local[*]")
      .appName("加载逻辑回归模型进行预测")
      .getOrCreate()

    import session.implicits._

    /** 1.数据准备 */
    val ds: Dataset[String] = session.read
      .textFile("D:\\workspace\\gitee_space\\spark-ml-machine-learning\\data\\breast-cancer-wisconsin.data")

    val data: Dataset[LabeledPoint] = ds.map(_.trim)
      .filter(line => !(line.isEmpty || line.contains("?")))
      .map(line => {
        val array: Array[Double] = line.split(",").map(_.toDouble)
        LabeledPoint(0, Vectors.dense(array.slice(1, array.size)))
      })

    /** 2.导入模型 */
    val model: PipelineModel = PipelineModel.load("./model/logisticregression")

    /** 3.模型预测 */
    val frame: DataFrame = model.transform(data)

    frame.show(5, false)

    session.stop()
  }
}
